﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace YuzKitabi.Data
{
    public class Baglanti : IDisposable
    {

        #region Const

        private const string _sunucu = ".";
        private const string _veritabani = "YuzKitabi";

        #endregion

        #region Degiskenler

        private SqlConnection _baglanti;
        private SqlCommand _komut;
        private SqlDataReader _verioku;
        private DataTable _tablo;


        #endregion

        #region Constructors

        public Baglanti()
        {
            string baglantiCümlesi = string.Format("Data Source={0};Initial Catolog={1};Integrated Security=true", _sunucu, _veritabani);
            _baglanti = new SqlConnection(baglantiCümlesi);
        }

        #endregion


        public string SpAdi
        {
            set
            {
                _komut = new SqlCommand(value, _baglanti);
                _komut.CommandType = CommandType.StoredProcedure;
            }
        }


        public string Komut
        {
            set
            {
                _komut = new SqlCommand(value, _baglanti);
            }
        }


        public void ParametreEkle(string parametreAd, SqlDbType tur, object deger)
        {
            _komut.Parameters.Add(parametreAd, tur).Value = deger;
        }


        public DataTable SorguCalistirTablo()
        {
            BaglantiAc();
            _verioku = null;
            _verioku = _komut.ExecuteReader();
            _tablo = new DataTable();
            _tablo.Load(_verioku);
            _baglanti.Close();
            return _tablo;

        }


        public object SorguCalistirBirKayit()
        {
            BaglantiAc();
            return _komut.ExecuteScalar();
        }

        public int SorguCalistir()
        {
            BaglantiAc();
            return _komut.ExecuteNonQuery();
        }



        #region Private Functions

        private void BaglantiAc()
        {
            if (_baglanti.State == ConnectionState.Closed)
                _baglanti.Open();
        }

        #endregion


        public void Dispose()
        {

        }
    }
}
